package com.fixme4trade.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import br.gov.frameworkdemoiselle.annotation.Startup;
import br.gov.frameworkdemoiselle.transaction.Transactional;

import com.fixme4trade.business.CustomerBC;
import com.fixme4trade.business.ForumBC;
import com.fixme4trade.business.MessageBC;
import com.fixme4trade.business.ProductBC;
import com.fixme4trade.business.SellBC;
import com.fixme4trade.business.SellerBC;
import com.fixme4trade.business.UserBC;
import com.fixme4trade.constant.Role;
import com.fixme4trade.domain.Customer;
import com.fixme4trade.domain.Forum;
import com.fixme4trade.domain.Message;
import com.fixme4trade.domain.Product;
import com.fixme4trade.domain.Seller;
import com.fixme4trade.domain.User;


public class ApplicationLoader {

	@Inject
	private ProductBC productBC;
	
	@Inject
	private UserBC userBC;
	
	@Inject
	private SellBC sellBC;
	
	@Inject
	private SellerBC sellerBC;
	
	@Inject
	private CustomerBC customerBC;
	
	@Inject
	private ForumBC forumBC;
	
	@Inject
	private MessageBC messageBC;
	
	
	@Startup(priority = 1)
	@Transactional
	public void loadData() {
		
		User userBob = new User();
		userBob.setUsername("bob");
		userBob.setPassword("bob");
		userBob.setFirstName("Bob");
		userBob.setFirstName("Marley");
		userBob.addRole(Role.CUSTOMER);	
		
		userBC.insert(userBob);					
		
		User userAlice = new User();
		userAlice.setUsername("alice");
		userAlice.setPassword("alice");
		userAlice.setFirstName("Alice");
		userAlice.setLastName("Cooper");
		userAlice.addRole(Role.SELLER);
		userBC.insert(userAlice);
		
		User userTrudy = new User();
		userTrudy.setUsername("trudy");
		userTrudy.setPassword("trudy");
		userTrudy.setFirstName("Trudy");
		userTrudy.setLastName("Evil");
		userTrudy.addRole(Role.CUSTOMER);
		userTrudy.addRole(Role.SELLER);
		userBC.insert(userTrudy);
				
		User admin = new User();
		admin.setUsername("admin");
		admin.setPassword("admin");
		admin.setFirstName("USER");
		admin.setLastName("ADMIN");
		admin.addRole(Role.ADMIN);
		userBC.insert(admin);
		
		List<User> vendorList = new ArrayList<User> ();
		vendorList.add(userAlice);		
		vendorList.add(userTrudy);					
				
		for (User vendor : vendorList ){
			
			Seller seller = sellerBC.getSellerByUser (vendor);
			
			if (seller != null) {			
				for (int i=0; i < 10; i++) {
					Product product = new Product();
					product.setItemName("Product "+i+" - From: "+vendor.getUsername());
					product.setDescription("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.");
					product.setQuantity(10);
					product.setPrice((i+1)*10);
					
					product.setOwner(seller);
					//seller.addProduct(product);
					productBC.insert(product);
					
					//sellerBC.update(seller);					
				}				
			}
		}
		
		/*		
		Customer customerBob = customerBC.getCustomerByUser(userBob);
		for (Product product : productBC.getProductsByQuery(null)){
			sellBC.sellProduct(product, customerBob, 1);
			break;
		}
		*/
		
		Forum forum = new Forum();
		forum.setForumTitle("Lorem Ipsum ...");
		forum.setOwner(userBob);
		forum.setCreationDate(new Date());
				
		Message message1 = new Message();
		message1.setMessageBody("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.");
		message1.setOwner(userBob);
		//messageBC.insert(message1);
		forum.addMessage(message1);		
						
		Message message2 = new Message();
		message2.setMessageBody("Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.");
		message2.setOwner(userAlice);		
		//messageBC.insert(message2);
		forum.addMessage(message2);
		
		Message message3 = new Message();
		message3.setMessageBody("Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.");
		message3.setOwner(userBob);		
		//messageBC.insert(message3);
		forum.addMessage(message3);
		
		Message message4 = new Message();
		message4.setMessageBody("Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius.");
		message4.setOwner(userAlice);		
		//messageBC.insert(message4);
		forum.addMessage(message4);
		
		Message message5 = new Message();
		message5.setMessageBody("Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.");
		message5.setOwner(userBob);		
		//messageBC.insert(message5);
		forum.addMessage(message5);
		
		forumBC.insert(forum);
	}
}